﻿/*
MetaCollection Schema Upgrade script for DOAITSDINVTEST
*/

GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
:setvar DatabaseName "PRODInventory"
:setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\"
:setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\"


GO
:on error exit
GO
USE [$(DatabaseName)]

GO
PRINT N'Dropping Full-text Index...';


GO
DROP FULLTEXT INDEX ON [structure].[ItemResource];


GO
PRINT N'Dropping Full-text Index...';


GO
DROP FULLTEXT INDEX ON [structure].[ItemProperty];


GO
PRINT N'Dropping Full-text Index...';


GO
DROP FULLTEXT INDEX ON [structure].[ItemVersion];


GO
PRINT N'Dropping [StructureTextIndex]...';


GO
DROP FULLTEXT CATALOG [StructureTextIndex];


GO
PRINT N'Dropping [structure].[Item].[_dta_stat_181575685_2_1]...';


GO
DROP STATISTICS [structure].[Item].[_dta_stat_181575685_2_1];


GO
PRINT N'Dropping [structure].[ItemVersion].[_dta_stat_37575172_6_1]...';


GO
DROP STATISTICS [structure].[ItemVersion].[_dta_stat_37575172_6_1];


GO
PRINT N'Dropping [structure].[ItemClaim].[_dta_index_ItemClaim_10_229575856__K3_1_4_5_6]...';


GO
DROP INDEX [_dta_index_ItemClaim_10_229575856__K3_1_4_5_6]
    ON [structure].[ItemClaim];


GO
PRINT N'Dropping [structure].[Item].[_dta_index_Item_10_181575685__K1_K2]...';


GO
DROP INDEX [_dta_index_Item_10_181575685__K1_K2]
    ON [structure].[Item];


GO
PRINT N'Dropping [structure].[ItemVersion].[_dta_index_ItemVersion_10_37575172__K1_K6]...';


GO
DROP INDEX [_dta_index_ItemVersion_10_37575172__K1_K6]
    ON [structure].[ItemVersion];


GO
PRINT N'Dropping DF__Item__Id__014935CB...';


GO
ALTER TABLE [structure].[Item] DROP CONSTRAINT [DF__Item__Id__014935CB];


GO
PRINT N'Dropping DF__ItemEvent__Event__7E6CC920...';


GO
ALTER TABLE [structure].[ItemEvent] DROP CONSTRAINT [DF__ItemEvent__Event__7E6CC920];


GO
PRINT N'Dropping DF__ItemClaim__Acces__07F6335A...';


GO
ALTER TABLE [structure].[ItemClaim] DROP CONSTRAINT [DF__ItemClaim__Acces__07F6335A];


GO
PRINT N'Dropping DF__ItemClaim__Id__07020F21...';


GO
ALTER TABLE [structure].[ItemClaim] DROP CONSTRAINT [DF__ItemClaim__Id__07020F21];


GO
PRINT N'Dropping FK_Event_Item...';


GO
ALTER TABLE [structure].[ItemEvent] DROP CONSTRAINT [FK_Event_Item];


GO
PRINT N'Dropping FK_Item_Item...';


GO
ALTER TABLE [structure].[Item] DROP CONSTRAINT [FK_Item_Item];


GO
PRINT N'Dropping FK_ItemClaim_Item...';


GO
ALTER TABLE [structure].[ItemClaim] DROP CONSTRAINT [FK_ItemClaim_Item];


GO
PRINT N'Dropping FK_ItemVersion_Item...';


GO
ALTER TABLE [structure].[ItemVersion] DROP CONSTRAINT [FK_ItemVersion_Item];


GO
PRINT N'Dropping FK_Event_EventType...';


GO
ALTER TABLE [structure].[ItemEvent] DROP CONSTRAINT [FK_Event_EventType];


GO
PRINT N'Dropping FK_ItemRelationship_ItemVersion...';


GO
ALTER TABLE [structure].[ItemRelationship] DROP CONSTRAINT [FK_ItemRelationship_ItemVersion];


GO
PRINT N'Dropping FK_ItemRelationship_RelationshipType...';


GO
ALTER TABLE [structure].[ItemRelationship] DROP CONSTRAINT [FK_ItemRelationship_RelationshipType];


GO
PRINT N'Dropping FK_ItemResource_ItemVersion...';


GO
ALTER TABLE [structure].[ItemResource] DROP CONSTRAINT [FK_ItemResource_ItemVersion];


GO
PRINT N'Dropping FK_ItemProperties_ItemVersion...';


GO
ALTER TABLE [structure].[ItemProperty] DROP CONSTRAINT [FK_ItemProperties_ItemVersion];


GO
PRINT N'Dropping FK_ItemVersion_ProviderType...';


GO
ALTER TABLE [structure].[ItemVersion] DROP CONSTRAINT [FK_ItemVersion_ProviderType];


GO
PRINT N'Dropping PK_ItemClaim...';


GO
ALTER TABLE [structure].[ItemClaim] DROP CONSTRAINT [PK_ItemClaim];


GO
PRINT N'Dropping [application].[ProviderType]...';


GO
IF EXISTS (SELECT 1 FROM sys.objects 
	WHERE OBJECT_ID = OBJECT_ID (N'[application].[ProviderType]') AND type = (N'U'))
	DROP TABLE [application].[ProviderType];


GO
PRINT N'Dropping [structure].[EventType]...';


GO
IF EXISTS (SELECT 1 FROM sys.objects 
	WHERE OBJECT_ID = OBJECT_ID (N'[structure].[EventType]') AND type = (N'U'))
DROP TABLE [structure].[EventType];


GO
PRINT N'Dropping [structure].[ItemProperty]...';


GO
IF EXISTS (SELECT 1 FROM sys.objects 
	WHERE OBJECT_ID = OBJECT_ID (N'[structure].[ItemProperty]') AND type = (N'U'))
DROP TABLE [structure].[ItemProperty];


GO
PRINT N'Dropping [structure].[RelationshipType]...';


GO
PRINT N'Dropping [structure].[GetPublishedOrLatestVersion]...';


GO
DROP FUNCTION [structure].[GetPublishedOrLatestVersion];


GO
PRINT N'Dropping [application]...';


GO
IF EXISTS (SELECT * FROM sys.schemas 
	WHERE name = '[application]')
	DROP SCHEMA [application];


GO
PRINT N'Creating [StructureService]...';


GO
CREATE USER [StructureService] WITHOUT LOGIN
    WITH DEFAULT_SCHEMA = [structure];

GO
PRINT N'Starting rebuilding table [structure].[ItemClaim]...';

GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

BEGIN TRANSACTION;

CREATE TABLE [structure].[tmp_ms_xx_ItemClaim] (
    [Id]            UNIQUEIDENTIFIER NOT NULL,
    [ClaimType]     VARCHAR (255) NOT NULL,
    [ClaimValue]    VARCHAR (50) NOT NULL,
    [AccessFlags]   INTEGER NOT NULL
);

IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[ItemClaim])
    BEGIN
        INSERT INTO [structure].[tmp_ms_xx_ItemClaim] ([Id], [ClaimType], [ClaimValue], [AccessFlags])
        SELECT	[structure].[Item].[SecuritySet],
				[structure].[ItemClaim].[ClaimType], 
				[structure].[ItemClaim].[ClaimValue], 
				[structure].[ItemClaim].[AccessFlags] 
		FROM    [structure].[ItemClaim] INNER JOIN [structure].[Item] ON [structure].[ItemClaim].[ItemId] = [structure].[Item].[Id]
		GROUP BY [structure].[ItemClaim].[ClaimType], [structure].[ItemClaim].[ClaimValue], [structure].[ItemClaim].[AccessFlags], [structure].[Item].[SecuritySet];
    END

DROP TABLE [structure].[ItemClaim];

EXECUTE sp_rename N'[structure].[tmp_ms_xx_ItemClaim]', N'ItemClaim';

GO
PRINT N'Creating PK_ItemClaim...';

GO
ALTER TABLE [structure].[ItemClaim]
    ADD CONSTRAINT [PK_ItemClaim] PRIMARY KEY CLUSTERED ([Id] ASC, [ClaimType] ASC, [ClaimValue] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);


COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Starting rebuilding table [structure].[Item]...';


GO
/*
The column [structure].[Item].[SecuritySet] is being dropped, data loss could occur.
*/
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

BEGIN TRANSACTION;

CREATE TABLE [structure].[tmp_ms_xx_Item] (
    [Id]               UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [ParentId]         UNIQUEIDENTIFIER NULL,
    [ExternalId]       VARCHAR (100)    NULL,
    [PublishedVersion] INT              NULL,
    [ItemClaimId]      UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [Href]             VARCHAR (2000)   NOT NULL
);

ALTER TABLE [structure].[tmp_ms_xx_Item]
    ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_Item] PRIMARY KEY CLUSTERED ([Id] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);

IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[Item])
    BEGIN
        INSERT INTO [structure].[tmp_ms_xx_Item] ([Id], [ParentId], [ExternalId], [PublishedVersion], [ItemClaimId], [Href])
        SELECT   [Id],
                 [ParentId],
                 [ExternalId],
                 [PublishedVersion],
				 [SecuritySet],
                 [Href]
        FROM     [structure].[Item]
        ORDER BY [Id] ASC;
    END

DROP TABLE [structure].[Item];

EXECUTE sp_rename N'[structure].[tmp_ms_xx_Item]', N'Item';

EXECUTE sp_rename N'[structure].[tmp_ms_xx_clusteredindex_PK_Item]', N'PK_Item', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [structure].[Item].[NonClusteredIndex]...';


GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex]
    ON [structure].[Item]([Id] ASC, [ParentId] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ONLINE = OFF, MAXDOP = 0)
    ON [PRIMARY];


GO
PRINT N'Starting rebuilding table [structure].[ItemEvent]...';


GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

BEGIN TRANSACTION;

CREATE TABLE [structure].[tmp_ms_xx_ItemEvent] (
    [Id]        INT              IDENTITY (1, 1) NOT NULL,
    [ItemId]    UNIQUEIDENTIFIER NOT NULL,
    [Version]   INT              NULL,
    [EventDate] DATETIME         DEFAULT (getdate()) NOT NULL,
    [EventType] VARCHAR (50)     NOT NULL,
    [Message]   VARCHAR (512)    NULL
);

ALTER TABLE [structure].[tmp_ms_xx_ItemEvent]
    ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_EventLog] PRIMARY KEY CLUSTERED ([Id] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);

IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[ItemEvent])
    BEGIN
        SET IDENTITY_INSERT [structure].[tmp_ms_xx_ItemEvent] ON;
        INSERT INTO [structure].[tmp_ms_xx_ItemEvent] ([Id], [ItemId], [Version], [EventDate], [EventType], [Message])
        SELECT   [Id],
                 [ItemId],
                 [Version],
                 [EventDate],
				 [EventTypeId],
                 [Message]
        FROM     [structure].[ItemEvent]
        ORDER BY [Id] ASC;
        SET IDENTITY_INSERT [structure].[tmp_ms_xx_ItemEvent] OFF;
    END


DROP TABLE [structure].[ItemEvent];

EXECUTE sp_rename N'[structure].[tmp_ms_xx_ItemEvent]', N'ItemEvent';

EXECUTE sp_rename N'[structure].[tmp_ms_xx_clusteredindex_PK_EventLog]', N'PK_EventLog', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Starting rebuilding table [structure].[ItemRelationship]...';

GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

BEGIN TRANSACTION;

CREATE TABLE [structure].[tmp_ms_xx_ItemRelationship] (
    [ItemId]           UNIQUEIDENTIFIER NOT NULL,
    [Version]          INT              NOT NULL,
    [RelationshipType] VARCHAR (50)     NOT NULL,
	[RelationshipType_temp] VARCHAR (50)     NOT NULL,
    [TargetItem]       VARCHAR (512)    NOT NULL,
    [State]            VARCHAR (1024)   NULL
);

ALTER TABLE [structure].[tmp_ms_xx_ItemRelationship]
    ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_ItemRelationship] PRIMARY KEY CLUSTERED ([ItemId] ASC, [Version] ASC, [RelationshipType] ASC, [TargetItem] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);

IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[ItemRelationship])
    BEGIN
        INSERT INTO [structure].[tmp_ms_xx_ItemRelationship] ([ItemId], [Version], [RelationshipType], [RelationshipType_temp], [TargetItem], [State])
        SELECT   [ItemId],
                 [Version],
				 [RelationshipTypeId],
				 [RelationshipTypeId], 
                 [TargetItem],
                 [State]
        FROM     [structure].[ItemRelationship]
        ORDER BY [ItemId] ASC, [Version] ASC, [TargetItem] ASC;
    END

DROP TABLE [structure].[ItemRelationship];

EXECUTE sp_rename N'[structure].[tmp_ms_xx_ItemRelationship]', N'ItemRelationship';

EXECUTE sp_rename N'[structure].[tmp_ms_xx_clusteredindex_PK_ItemRelationship]', N'PK_ItemRelationship', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

GO
IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[ItemRelationShip])
    BEGIN
		UPDATE [structure].[ItemRelationShip]
		SET [RelationShipType] =
		(SELECT [Name]
		FROM [structure].[RelationShipType]
		WHERE [ItemRelationShip].[RelationshipType_temp] = [RelationShipType].[Id]);
    END
    
GO
IF EXISTS (SELECT 1 FROM sys.objects 
	WHERE OBJECT_ID = OBJECT_ID (N'[structure].[RelationshipType]') AND type = (N'U'))
	DROP TABLE [structure].[RelationshipType];

GO
ALTER TABLE [structure].[ItemRelationShip]
	DROP COLUMN [RelationshipType_temp];

GO
PRINT N'Starting rebuilding table [structure].[ItemVersion]...';


GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET XACT_ABORT ON;

BEGIN TRANSACTION;

CREATE TABLE [structure].[tmp_ms_xx_ItemVersion] (
    [ItemId]          UNIQUEIDENTIFIER NOT NULL,
    [Version]         INT              NOT NULL,
    [LeftVersion]     INT              NULL,
    [RightVersion]    INT              NULL,
    [Comment]         VARCHAR (255)    NULL,
    [Name]            VARCHAR (255)    NOT NULL,
    [MimeType]        VARCHAR (50)     NOT NULL,
    [SpecificationId] UNIQUEIDENTIFIER NOT NULL,
    [CreatedBy]       VARCHAR (255)    NOT NULL,
    [LastModified]    DATETIME         NOT NULL,
    [PropertyData]    VARBINARY (MAX)    NULL,
    [RowIdentifier]   UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL
);

ALTER TABLE [structure].[tmp_ms_xx_ItemVersion]
    ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_ItemVersion] PRIMARY KEY CLUSTERED ([ItemId] ASC, [Version] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);

IF EXISTS (SELECT TOP 1 1
           FROM   [structure].[ItemVersion])
    BEGIN
        INSERT INTO [structure].[tmp_ms_xx_ItemVersion] ([ItemId], [Version], [LeftVersion], [RightVersion], [Comment], [Name], [MimeType], [SpecificationId], [CreatedBy], [LastModified], [RowIdentifier], [PropertyData])
        SELECT   [ItemId],
                 [Version],
                 [LeftVersion],
                 [RightVersion],
                 [Comment],
                 [Name],
                 [MimeType],
				 [ProviderTypeId],
                 [CreatedBy],
                 [LastModified],
                 [RowIdentifier],
                 [PropertyData]
        FROM     [structure].[ItemVersion]
        ORDER BY [ItemId] ASC, [Version] ASC;
    END

DROP TABLE [structure].[ItemVersion];

EXECUTE sp_rename N'[structure].[tmp_ms_xx_ItemVersion]', N'ItemVersion';

EXECUTE sp_rename N'[structure].[tmp_ms_xx_clusteredindex_PK_ItemVersion]', N'PK_ItemVersion', N'OBJECT';

COMMIT TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;


GO
PRINT N'Creating [structure].[ItemVersion].[ItemVersionRowIndex]...';


GO
CREATE UNIQUE NONCLUSTERED INDEX [ItemVersionRowIndex]
    ON [structure].[ItemVersion]([RowIdentifier] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ONLINE = OFF, MAXDOP = 0)
    ON [PRIMARY];


GO
PRINT N'Creating [structure].[ItemVersion].[NonClusteredIndex]...';


GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex]
    ON [structure].[ItemVersion]([ItemId] ASC, [Name] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ONLINE = OFF, MAXDOP = 0)
    ON [PRIMARY];


GO
PRINT N'Creating DF__ItemClaim__Acces__0AD2A005...';


GO
ALTER TABLE [structure].[ItemClaim]
    ADD DEFAULT ((0)) FOR [AccessFlags];


GO
PRINT N'Creating [structure].[Item].[IdParentId]...';


GO
CREATE STATISTICS [IdParentId]
    ON [structure].[Item]([ParentId], [Id]);


GO
PRINT N'Creating [structure].[ItemVersion].[NameItemId]...';


GO
CREATE STATISTICS [NameItemId]
    ON [structure].[ItemVersion]([Name], [ItemId]);


GO
PRINT N'Creating FK_Event_Item...';


GO
ALTER TABLE [structure].[ItemEvent] WITH NOCHECK
    ADD CONSTRAINT [FK_Event_Item] FOREIGN KEY ([ItemId]) REFERENCES [structure].[Item] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;


GO
PRINT N'Creating FK_Item_Item...';


GO
ALTER TABLE [structure].[Item] WITH NOCHECK
    ADD CONSTRAINT [FK_Item_Item] FOREIGN KEY ([ParentId]) REFERENCES [structure].[Item] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;


GO
PRINT N'Creating FK_ItemVersion_Item...';


GO
ALTER TABLE [structure].[ItemVersion] WITH NOCHECK
    ADD CONSTRAINT [FK_ItemVersion_Item] FOREIGN KEY ([ItemId]) REFERENCES [structure].[Item] ([Id]) ON DELETE CASCADE ON UPDATE CASCADE;


GO
PRINT N'Creating FK_ItemRelationship_ItemVersion...';


GO
ALTER TABLE [structure].[ItemRelationship] WITH NOCHECK
    ADD CONSTRAINT [FK_ItemRelationship_ItemVersion] FOREIGN KEY ([ItemId], [Version]) REFERENCES [structure].[ItemVersion] ([ItemId], [Version]) ON DELETE NO ACTION ON UPDATE NO ACTION;


GO
PRINT N'Creating FK_ItemResource_ItemVersion...';


GO
ALTER TABLE [structure].[ItemResource] WITH NOCHECK
    ADD CONSTRAINT [FK_ItemResource_ItemVersion] FOREIGN KEY ([ItemId], [Version]) REFERENCES [structure].[ItemVersion] ([ItemId], [Version]) ON DELETE NO ACTION ON UPDATE NO ACTION;


GO
PRINT N'Altering [structure].[GetItemPath]...';


GO
ALTER FUNCTION [structure].[GetItemPath]
(
	@itemId uniqueidentifier, 
	@version int
)
RETURNS varchar(500)
AS
BEGIN
DECLARE @Path varchar(500)

;WITH PathParts (ParentId, ItemId, ObjectPath)
AS
(
    SELECT 
		structure.Item.ParentId,
		structure.Item.Id,
		CAST(structure.ItemVersion.Name as varchar(255))
    FROM
		structure.Item
		INNER JOIN
		structure.ItemVersion
		ON
		(structure.Item.Id = structure.ItemVersion.ItemId)
    WHERE
		structure.Item.Id = @itemId
	AND
		(structure.ItemVersion.Version = @version)
    UNION ALL
    SELECT
		structure.Item.ParentId,
		structure.Item.Id,
		CAST (structure.ItemVersion.Name + '/' + PathParts.ObjectPath AS varchar(255))
    FROM
		structure.Item
		INNER JOIN
		structure.ItemVersion
		ON
		(structure.Item.Id = structure.ItemVersion.ItemId)
			INNER JOIN
			PathParts
			ON
			structure.Item.Id = PathParts.ParentId
    WHERE
		(structure.ItemVersion.Version =  structure.Item.PublishedVersion)
)
 

SELECT @Path =  'col://' + (objectpath ) FROM PathParts WHERE ParentId IS NULL
RETURN @Path
END
GO
PRINT N'Checking existing data against newly created constraints';


GO
USE [$(DatabaseName)];


GO
ALTER TABLE [structure].[ItemEvent] WITH CHECK CHECK CONSTRAINT [FK_Event_Item];

ALTER TABLE [structure].[Item] WITH CHECK CHECK CONSTRAINT [FK_Item_Item];

ALTER TABLE [structure].[ItemVersion] WITH CHECK CHECK CONSTRAINT [FK_ItemVersion_Item];

ALTER TABLE [structure].[ItemRelationship] WITH CHECK CHECK CONSTRAINT [FK_ItemRelationship_ItemVersion];

ALTER TABLE [structure].[ItemResource] WITH CHECK CHECK CONSTRAINT [FK_ItemResource_ItemVersion];


GO
